//
//  LCWPageViewCell.swift
//  MyWB
//
//  Created by 李成万 on 16/7/5.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

class LCWPageViewCell: UICollectionViewCell {
    
    var indexPath : NSIndexPath?{
        didSet{
            messageLabel.text = "\(indexPath?.section ?? 0)组\(indexPath?.item ?? 0)个"
        }
    }
    var btnArr : [LCWEmotionBtn] = [LCWEmotionBtn]()
    
    var emotions : [LCWEmotions]?{
        didSet{
            
            for btn in btnArr{
                btn.hidden = true
                
            }
            
            for (i,emotion) in emotions!.enumerate(){
                let btn = btnArr[i]
                btn.hidden = false
                btn.emotion = emotion
                if emotion.isEmoji {
                    let title = ((emotion.code ?? "") as NSString).emoji()
                    
                    btn.setTitle(title, forState: UIControlState.Normal)
                    
                    btn.setImage(nil, forState: UIControlState.Normal)
                }else{
                    let path = emotion.path ?? ""
                    let png = emotion.png ?? ""
                    let name = path + png
                    let image = UIImage(named: name, inBundle: NSBundle(path: NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!), compatibleWithTraitCollection: nil)
                    
                    btn.setImage(image, forState: UIControlState.Normal)
                    btn.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var messageLabel : UILabel = UILabel(messageColor: UIColor.redColor(), textFont: 25)
    
    private lazy var deleteBtn : LCWEmotionBtn = {
        let btn = LCWEmotionBtn(imageName: "compose_emotion_delete", backgroundImageName: "")
        
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
      
        
        return btn
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
//        addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
        addBtn()
        
        contentView.addSubview(deleteBtn)
        
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = (SCREENW - 10) / CGFloat(EmotionPageViewCol)
        let h = (216 - 37 - 20) / CGFloat(EmotionPageViewrow)
        
        for (i,btn) in btnArr.enumerate(){
            let numberX = i % EmotionPageViewCol
            let numberY = i / EmotionPageViewCol
            
            btn.frame = CGRect(x: w * CGFloat(numberX) + 5, y: h * CGFloat(numberY), width: w, height: h)
        }
        
        deleteBtn.frame = CGRect(x: SCREENW - 5 - w, y: h * 2, width: w, height: h)
    }
    
    func addBtn(){
        for _ in 0..<EmotionPageViewCount{
            let btn = LCWEmotionBtn()
//            btn.backgroundColor = randomColor()
            
            btn.addTarget(self, action: "emotionBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            btnArr.append(btn)
            contentView.addSubview(btn)
        }
    }
    @objc private func deleteBtnClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(deleteTextView, object: nil)
    }
    
    @objc private func emotionBtnClick(btn:LCWEmotionBtn){
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionShow, object: btn.emotion)
    }
}
